package com.reliable.service.controller;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reliable.api.dto.ReliableExportPushRecord.ReliableExportPushRecordDetailDTO;
import com.reliable.api.param.IdParam;
import com.reliable.api.param.ReliablePushTemplate.ReliablePushDetailPageParam;
import com.reliable.api.param.ReliablePushTemplate.ReliablePushTemplatePageParam;
import com.reliable.api.param.ReliablePushTemplate.ReliablePushTemplateUpdateParam;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.core.page.TableDataInfo;
import com.reliable.service.utils.IdEncryptUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.reliable.service.domain.ReliablePushTemplate;
import com.reliable.service.service.IReliablePushTemplateService;
import com.reliable.api.dto.ReliableExportPushRecord.ReliablePushTemplateStatDTO;

/**
 * 推送目标模板Controller
 * 
 * @author ruoyi
 * @date 2025-10-29
 */
@RestController
@RequestMapping("/reliable/pushTemplate")
@Api(value = "推送目标模板控制器", tags = {"推送目标模板管理"})
public class ReliablePushTemplateController {

    @Autowired
    private IReliablePushTemplateService reliablePushTemplateService;

    /**
     * 查询推送目标模板列表
     */
    @ApiOperation("查询推送目标模板列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ReliablePushTemplatePageParam param) {
        // 获取查询结果
        List<ReliablePushTemplateStatDTO> list = reliablePushTemplateService.selectReliablePushTemplateStatList(param);

        for (ReliablePushTemplateStatDTO reliablePushTemplateStatDTO : list) {
            reliablePushTemplateStatDTO.setExportBusinessId(IdEncryptUtils.encrypt(reliablePushTemplateStatDTO.getExportId()));
        }


        // 手动分页处理
        int fromIndex = (int) ((param.getPageNum() - 1) * param.getPageSize());
        int toIndex = (int) Math.min(fromIndex + param.getPageSize(), list.size());
        
        // 创建分页结果
        List<ReliablePushTemplateStatDTO> pageList = list.subList(
            Math.max(0, fromIndex), 
            Math.max(0, toIndex)
        );
        
        TableDataInfo tableDataInfo = new TableDataInfo(pageList, list.size());
        tableDataInfo.setCode(200); // 设置成功状态码
        return tableDataInfo;
    }


    /**
     * 获取推送目标模板详细信息
     */
    @ApiOperation("获取推送目标模板详细信息")
    @PostMapping(value = "/info")
    public AjaxResult getInfo(@RequestBody IdParam param) {

        ReliablePushTemplate template = reliablePushTemplateService.getById(IdEncryptUtils.decrypt(param.getBusinessId()));
        return AjaxResult.success(template);
    }

    /**
     * 获取推送目标模板信息列表，根据exportid查询
     */
    @ApiOperation("获取推送目标模板信息列表，根据exportid查询")
    @PostMapping("/listByExportId")
    public AjaxResult listByExportId(@RequestBody ReliablePushDetailPageParam param) {
       return reliablePushTemplateService.pushDetailList(param);
    }

    /**
     * 新增推送目标模板
     */
    @ApiOperation("新增推送目标模板")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ReliablePushTemplate reliablePushTemplate) {
        return toAjax(reliablePushTemplateService.save(reliablePushTemplate));
    }

    /**
     * 修改推送目标模板
     */
    @ApiOperation("修改推送目标模板")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ReliablePushTemplateUpdateParam param) {
        param.setId(IdEncryptUtils.decrypt(param.getBusinessId()));
        ReliablePushTemplate reliablePushTemplate = new ReliablePushTemplate();
        BeanUtils.copyProperties(param, reliablePushTemplate);
        return toAjax(reliablePushTemplateService.updateById(reliablePushTemplate));
    }

    /**
     * 删除推送目标模板
     */
    @ApiOperation("删除推送目标模板")
    @PostMapping("/delete")
    public AjaxResult remove(@RequestBody IdParam param) {
        return toAjax(reliablePushTemplateService.removeById(IdEncryptUtils.decrypt(param.getBusinessId())));
    }

    /**
     * 修改启用状态
     */
    @ApiOperation("修改启用状态")
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody IdParam param) {
        ReliablePushTemplate template = reliablePushTemplateService.getById(IdEncryptUtils.decrypt(param.getBusinessId()));
        template.setTemplateStatus(template.getTemplateStatus() == 1 ? 2 : 1);
        template.setUpdateTime(new Date());
        return toAjax(reliablePushTemplateService.updateById(template));
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean rows) {
        return rows ? AjaxResult.success() : AjaxResult.error();
    }
}